*
* Prepare data for applicant-level analysis
*
* Inputs:
*	"${data_for_welfare_analysis_${year}}"
*	"${data_contract_dictionary_${year}}"
*	"${match_no_dominated_lower_${year}}"
*	"${match_no_dominated_upper_${year}}"
*	"${data_applicants_tidy_${year}}"
*	"${raw_data_dir}/Income.dta"
*	"${raw_data_dir}/NUTS_unemployment.dta"
*
* Output:
*	"${result_no_dominated_${year}}"
*

use "${data_for_welfare_analysis_${year}}", clear

bys applicant_id: egen admitted_applicant = max(admitted)
keep if (rank == 1 & admitted_applicant == 0) | (admitted == 1)
replace contract_id = "None" if admitted == 0
replace rank = 100 if admitted == 0
replace state_funded = . if admitted == 0

keep id applicant_id contract_id rank program_id state_funded has_justified_envy admitted_below_ps_cutoff

merge n:1 contract_id using "${data_contract_dictionary_${year}}", keepusing(contract_id)
drop if _m == 2
drop _m

* ------------------------------------------------------------------------------
* Add match: state-funded above self-funded ------------------------------------
* ------------------------------------------------------------------------------
merge 1:1 id applicant_id using "${match_no_dominated_lower_${year}}"

gen contract_id_lower = contract_id_spda_cf
gen rank_lower = rank_spda_cf
gen state_funded_lower = state_funded_spda_cf
gen program_id_lower = program_id_spda_cf
replace contract_id_lower = contract_id if _m == 1 & (has_justified_envy == 1 | admitted_below_ps_cutoff == 1)
replace contract_id_lower = "None" if _m == 1 & (has_justified_envy != 1 & admitted_below_ps_cutoff != 1) 
replace program_id_lower = program_id if _m == 1 & (has_justified_envy == 1 | admitted_below_ps_cutoff == 1)
replace program_id_lower = "None" if _m == 1 & (has_justified_envy != 1 & admitted_below_ps_cutoff != 1) 
replace rank_lower = rank if _m == 1 & (has_justified_envy == 1 | admitted_below_ps_cutoff == 1)
replace rank_lower = 100 if _m == 1 & (has_justified_envy != 1 & admitted_below_ps_cutoff != 1) 
replace state_funded_lower = state_funded if _m == 1 & (has_justified_envy == 1 | admitted_below_ps_cutoff == 1)
replace state_funded_lower = . if _m == 1 & (has_justified_envy != 1 & admitted_below_ps_cutoff != 1) 
drop _m
drop *_spda_cf
	
* ------------------------------------------------------------------------------
* Add match: state-funded on top -----------------------------------------------
* ------------------------------------------------------------------------------
merge 1:1 id applicant_id using "${match_no_dominated_upper_${year}}"

gen contract_id_upper = contract_id_spda_cf
gen rank_upper = rank_spda_cf
gen state_funded_upper = state_funded_spda_cf
gen program_id_upper = program_id_spda_cf
replace contract_id_upper = contract_id if _m == 1 & (has_justified_envy == 1 | admitted_below_ps_cutoff == 1)
replace contract_id_upper = "None" if _m == 1 & (has_justified_envy != 1 & admitted_below_ps_cutoff != 1) 
replace program_id_upper = program_id if _m == 1 & (has_justified_envy == 1 | admitted_below_ps_cutoff == 1)
replace program_id_upper = "None" if _m == 1 & (has_justified_envy != 1 & admitted_below_ps_cutoff != 1) 
replace rank_upper = rank if _m == 1 & (has_justified_envy == 1 | admitted_below_ps_cutoff == 1)
replace rank_upper = 100 if _m == 1 & (has_justified_envy != 1 & admitted_below_ps_cutoff != 1) 
replace state_funded_upper = state_funded if _m == 1 & (has_justified_envy == 1 | admitted_below_ps_cutoff == 1)
replace state_funded_upper = . if _m == 1 & (has_justified_envy != 1 & admitted_below_ps_cutoff != 1) 
drop _m
drop *_spda_cf

* ------------------------------------------------------------------------------
* Identify winners and losers --------------------------------------------------
* ------------------------------------------------------------------------------
gen winner_lower = rank_lower < rank & contract_id != contract_id_lower
lab var winner_lower "Winner (state-funded above self-funded)"

gen loser_lower = rank_lower > rank & contract_id != contract_id_lower
lab var loser_lower "Loser (state-funded above self-funded)"	

gen winner_upper = rank_upper < rank & contract_id != contract_id_upper
lab var winner_upper "Winner (state-funded on top)"

gen loser_upper = rank_upper > rank & contract_id != contract_id_upper
lab var loser_upper "Loser (state-funded on top)"	

* ------------------------------------------------------------------------------
* Merge applicant characteristics ----------------------------------------------
* ------------------------------------------------------------------------------
merge 1:1 id applicant_id using "${data_applicants_tidy_${year}}", keepusing( ///
	applicant_id female age sec_school voc_school capital countycapital town village GPA11 GPA11_std ///
	GPA11_missing disadv contract_num contract_num_data program_num_data ///
	ROL_state_self_funded ROL_state_funded_only ROL_self_funded_onl year ///
	eligibility sample_hss any_ba_listed NUTSregion tazon ///
	dominated_dropping_applicant_id dominated_flipping_applicant_id ///
	costly_dominated_drop_DA_id costly_dominated_drop_id ///
	costly_dominated_flip_DA_id costly_dominated_flip_id ///
	no_funded_version_at_all all_state_f_above_self_f fulltime_max)
drop _m

* ------------------------------------------------------------------------------
* Merge income -----------------------------------------------------------------
* ------------------------------------------------------------------------------
merge n:1 year tazon using "${raw_data_income}"
drop if _merge == 2
drop _merge

replace grossincome = grossincome*300/202.26 if year == 2009
replace grossincome = grossincome*300/208.15 if year == 2010
replace grossincome = grossincome*300/200.94 if year == 2011
replace grossincome = grossincome*300/225.37 if year == 2012
replace grossincome = grossincome*300/223.70 if year == 2013
replace grossincome = grossincome*300/232.52 if year == 2014

label variable grossincome "Gross annual per capita income (1000 USD)"

gen grossincome_missing = grossincome == .
label variable grossincome_missing "Gross annual per capita income - missing"

* ------------------------------------------------------------------------------
* Merge unemployment rate ------------------------------------------------------
* ------------------------------------------------------------------------------
merge n:1 NUTSregion using "${raw_data_unemployment}"
drop if _m == 2
drop _m
gen unempl_missing = unempl_rate == .

label variable unempl_rate "Unemployment rate in 2008 (\%)"
label variable unempl_missing "Unemployment rate in 2008 - missing"

* ------------------------------------------------------------------------------
* Save applicant-level data ----------------------------------------------------
* ------------------------------------------------------------------------------
save "${result_no_dominated_${year}}", replace
